package com.xexun.gpstracker.pojo;

import java.io.Serializable;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONObject;

import com.lurencun.service.autoupdate.Version;
import com.xexun.gpstracker.AppContext;
import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.common.CommonRestPath;
import com.xexun.gpstracker.common.Constants;
import com.xexun.gpstracker.dialog.WizardAlertDialog;
import com.xexun.gpstracker.inter.CommCallback;
import com.xexun.gpstracker.inter.HttpCallback;
import com.xexun.gpstracker.util.HttpUtil;
import com.xexun.gpstracker.util.JSONHelper;
import com.xexun.gpstracker.util.JsonHttpResponseHelper;
import com.xexun.gpstracker.util.Utils;
import com.xexun.gpstracker.vo.GTLatLng;
import com.xexun.gpstracker.vo.Session;


public class User implements Serializable {

    private static String TAG = User.class.getSimpleName();

    private static final long serialVersionUID = 1685006415337227032L;

    private Long id;
    private String phone;
    private String portrait;
    private String pwd;
    private String imei;
    private String appUserNum;    //为1时为主用户
    private String term_hone; //终端手机号

    public User() {
    }

    public User(JSONObject json) {
    }

    /**
     * @return
     * @deprecated 有可能为null，建议使用imei来代替
     */
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTerm_hone() {
        return term_hone;
    }

    public void setTerm_hone(String term_hone) {
        this.term_hone = term_hone;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getAppUserNum() {
        return appUserNum;
    }

    public void setAppUserNum(String appUserNum) {
        this.appUserNum = appUserNum;
    }

    public String getPortrait() {
        return portrait;
    }

    public void setPortrait(String portrait) {
        this.portrait = portrait;
    }

    public String getImei() {
        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public static void checkPhone(Context context, String phone, final HttpCallback callback) {
        try {
            JSONObject json = new JSONObject();
            json.put("phone", phone);
            HttpUtil.post(CommonRestPath.checkPhone(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure(null);
            } catch (Exception e) {
                Log.e(TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }

    public static void getValdcode(Context context, String phone, final HttpCallback callback) {
        try {
            JSONObject json = new JSONObject();
            json.put("phone", phone);
            HttpUtil.post(CommonRestPath.validCode(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure(null);
            } catch (Exception e) {
                Log.e(TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }

    public static void register(Context context, String phone, String password, String code, String imei, int watchRegion, String rel, String wPhone, float timezone, final HttpCallback callback) {
        try {
            JSONObject json = new JSONObject();
            json.put("phone", phone);
            json.put("password", Utils.enctype(password));
            json.put("regImei", imei);
            json.put("validcode", code);
            json.put("watchRegion", watchRegion);
            json.put("relation", rel);
            json.put("watchPhone", wPhone);
            json.put("timeZone", timezone);
            json.put("clientId", Session.getInstance().getClientId());
//			json.put("term_hone", term_hone);
            HttpUtil.post(CommonRestPath.register(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure(null);
            } catch (Exception e) {
                Log.e(TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }


    public static void getServerIP(Context context, String key, final HttpCallback callback) {
        try {
            JSONObject json = new JSONObject();
            json.put("key", key);
            Log.i(TAG,"clientid:"+Session.getInstance().getClientId());
            HttpUtil.postDefault(CommonRestPath.getServerIP(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure(null);
            } catch (Exception e) {
                Log.e(Constants.TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }


//    static final String LOGINURL = "http://192.168.1.242:8811/user/login";
    public static void login(Context context, String phone, String password, final HttpCallback callback) {
        try {
            JSONObject json = new JSONObject();
            json.put("phone", phone);
            json.put("password", Utils.enctype(password));
            json.put("clientid", Session.getInstance().getClientId());
            Log.i(TAG,"clientid:"+Session.getInstance().getClientId());
//       HttpUtil.post(LOGINURL, json, new JsonHttpResponseHelper(callback).getHandler());
            HttpUtil.post(CommonRestPath.login(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure(null);
            } catch (Exception e) {
                Log.e(TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }

    public static void modifyPassword(Context context, String phone, String password, String code, String imei, final HttpCallback callback) {
        try {
            JSONObject json = new JSONObject();
            json.put("phone", phone);
            json.put("password", Utils.enctype(password));
            json.put("validCode", code);
            json.put("imei", imei);
            HttpUtil.post(CommonRestPath.updatePwd(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure(null);
            } catch (Exception e) {
                Log.e(TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }

    public static void logout(Context context, final HttpCallback callback) {
        try {
            JSONObject json = new JSONObject();
            HttpUtil.post(CommonRestPath.logout(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure(null);
            } catch (Exception e) {
                Log.e(TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }

    public static void sendFeedback(Context context, JSONObject json, final HttpCallback callback) {
        try {
            HttpUtil.post(CommonRestPath.sendFeedback(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure(null);
            } catch (Exception e) {
                Log.e(TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }

    public static void getAppVersion(final Context context, final boolean showDialog, final CommCallback callback) {
        try {
            HttpCallback httpCallback = new HttpCallback() {
                @Override
                public void onStart() {
                    if (showDialog) {
                        WizardAlertDialog.getInstance().showProgressDialog(R.string.check_new_version, context);
                    }
                }

                @Override
                public void onFinish() {
                    WizardAlertDialog.getInstance().closeProgressDialog();
                }

                @Override
                public void onSuccess(ResponseData obj) {
                    if (obj.getRetcode() == 1) {
                        try {
                            JSONObject jsonObject = obj.getBody();
                            int versionCode = JSONHelper.getInt(jsonObject, "versionCode");
                            String versionName = JSONHelper.getString(jsonObject, "versionName");
                            String feature = JSONHelper.getString(jsonObject, "releaseNote");
                            String releaseUrl = JSONHelper.getString(jsonObject, "releaseUrl");
                            int minVersion = JSONHelper.getInt(jsonObject, "minVersionCode");

                            int versionCodeIntel = JSONHelper.getInt(jsonObject, "versionCodeIntel");
                            String versionNameIntel = JSONHelper.getString(jsonObject, "versionNameIntel");
                            String featureIntel = JSONHelper.getString(jsonObject, "releaseNoteIntel");
                            String releaseUrlIntel = JSONHelper.getString(jsonObject, "releaseUrlIntel");
                            PackageInfo info = context.getPackageManager().getPackageInfo(context.getApplicationContext().getPackageName(), 0);
                            SharedPreferences sp = Utils.getPreferences(context);

                            Version version = new Version(versionCode, versionName, feature, releaseUrl);
                            Version versionIntel = new Version(versionCodeIntel, versionNameIntel, featureIntel, releaseUrlIntel);
                            boolean hasNewVersion = version.code > info.versionCode;
                            boolean hasNewIntelVersion = versionIntel.code > info.versionCode && versionIntel.code > version.code;
                            boolean useIntel = sp.getBoolean(Constants.SETTING_VERSION_CHOICE_INTEL, false);

                            //0 无最新版本，1 有最新版本，2 版本过旧，需要强制升级后才能使用
                            if (useIntel && hasNewIntelVersion) {
                                versionIntel.testVersion = true;
                                callback.execute(versionIntel);
                            } else if (hasNewVersion && info.versionCode < minVersion) {
                                version.forceUpdate = true;
                                callback.execute(version);
                            } else if (hasNewVersion) {
                                callback.execute(version);
                            } else {
                                callback.execute(null);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        Toast.makeText(context, obj.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(String message) {
                    if (showDialog) {
                        Toast.makeText(context, R.string.update_failed, Toast.LENGTH_SHORT).show();
                    }
                }
            };

            HttpUtil.get(CommonRestPath.getVersion(), null, new JsonHttpResponseHelper(httpCallback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.e(TAG, AppContext.getContext().getString(R.string.callback_err), ex);
        }
    }

    public static void getChatGIFVersion(Context context, final HttpCallback callback) {
        try {
            HttpUtil.post(CommonRestPath.getVersion(), (new JSONObject()), new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure(null);
            } catch (Exception e) {
                Log.e(TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }

    public static void postGPS() {
        try {
            GTLatLng latLng = Session.getInstance().getLatLng();
            if (latLng == null) {
                return;
            }
            String gps = String.format("g,%s,%s", latLng.latitude + "", latLng.longitude + "");
            String wifi = String.format("%s", Session.getInstance().getWifiInfo());
            String cell = String.format("l,", Session.getInstance().getGsmCellInfo());
            JSONObject json = new JSONObject();
            json.put("gpsInfro", gps);
            json.put("wifiInfro", wifi);
            json.put("lbsInfro", cell);
            HttpCallback callback = new HttpCallback() {
                @Override
                public void onStart() {
                }

                @Override
                public void onFinish() {
                }

                @Override
                public void onSuccess(ResponseData obj) {
                }

                @Override
                public void onFailure(String message) {
                }
            };
            HttpUtil.post(CommonRestPath.postPosition(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            Log.e(TAG, "提交数据出错！", ex);
        }

    }

}
